package com.iknow.net.translate.impl;

import com.iknow.IKnow;
import com.iknow.IKnowApiV4;
import com.iknow.data.Word;
import com.iknow.data.Word.SentInfo;
import com.iknow.file.CacheSystem;
import com.iknow.http.HttpException;
import com.iknow.http.Response;
import com.iknow.http.ResponseException;
import com.iknow.net.translate.IKTranslateManager.IKTranslate;
import com.iknow.util.DomXmlUtil;
import com.iknow.util.StringUtil;
import java.util.List;
import org.w3c.dom.Document;
import org.w3c.dom.Element;
import org.w3c.dom.Node;
import org.w3c.dom.NodeList;

public class IKDictTranslate
  implements IKTranslateManager.IKTranslate
{
  private static final String url = "http://dict.cn/ws.php?utf8=true&q=";

  private String getUrl(String paramString, boolean paramBoolean)
  {
    String str;
    if (paramBoolean)
      str = "http://dict.cn/ws.php?utf8=true&q=[" + paramString + "]";
    while (true)
    {
      return str;
      str = "http://dict.cn/ws.php?utf8=true&q=" + paramString;
    }
  }

  private Element request(String paramString)
  {
    Element localElement1;
    Element localElement2;
    try
    {
      localElement2 = IKnow.mApi.get(paramString, false).asDocument().getDocumentElement();
      localElement1 = localElement2;
      return localElement1;
    }
    catch (ResponseException localResponseException)
    {
      localResponseException.printStackTrace();
      localElement1 = null;
    }
    catch (HttpException localHttpException)
    {
      while (true)
        localHttpException.printStackTrace();
    }
  }

  private Element requestLocal(String paramString)
  {
    Object localObject;
    String str;
    try
    {
      str = IKnow.mCacheSystem.readFile(paramString);
      if (str == null)
      {
        localObject = null;
      }
      else
      {
        Response localResponse = new Response();
        localResponse.setResponseAsString(str);
        Element localElement = localResponse.asDocument().getDocumentElement();
        localObject = localElement;
      }
    }
    catch (ResponseException localResponseException)
    {
      localResponseException.printStackTrace();
      localObject = null;
    }
    return localObject;
  }

  public void fillTranslate(Element paramElement, Word paramWord)
  {
    paramWord.setUserId("1");
    paramWord.setKey(DomXmlUtil.getTagItemValue(paramElement, "key"));
    paramWord.setLang(DomXmlUtil.getTagItemValue(paramElement, "lang"));
    paramWord.setAudioUrl(DomXmlUtil.getTagItemValue(paramElement, "audio"));
    paramWord.setPron(DomXmlUtil.getTagItemValue(paramElement, "pron"));
    paramWord.setDef(DomXmlUtil.getTagItemValue(paramElement, "def"));
    paramWord.setWordMemo(DomXmlUtil.getTagItemValue(paramElement, "memo"));
    List localList = paramWord.getSentList();
    NodeList localNodeList = paramElement.getElementsByTagName("sent");
    int i = 0;
    while (true)
    {
      if (i >= localNodeList.getLength())
        return;
      Node localNode = localNodeList.item(i);
      Word.SentInfo localSentInfo = new Word.SentInfo();
      String str = DomXmlUtil.getTagItemValue(localNode, "orig");
      if (str != null)
        str = str.replace("<em>", "").replace("</em>", "");
      localSentInfo.setOrig(str);
      localSentInfo.setTrans(DomXmlUtil.getTagItemValue(localNode, "trans"));
      localList.add(localSentInfo);
      ++i;
    }
  }

  public void fillTranslateSugg(Element paramElement, Word paramWord)
  {
    List localList = paramWord.getSuggList();
    NodeList localNodeList = paramElement.getElementsByTagName("sugg");
    int i = 0;
    while (true)
    {
      if (i >= localNodeList.getLength())
        return;
      localList.add(DomXmlUtil.getCurrentText(localNodeList.item(i)));
      ++i;
    }
  }

  public Word translate(String paramString, boolean paramBoolean)
  {
    Word localWord = new Word();
    if (StringUtil.isEmpty(paramString));
    while (true)
    {
      return localWord;
      Element localElement1 = request(getUrl(paramString, false));
      if (localElement1 != null)
        fillTranslate(localElement1, localWord);
      if ((paramBoolean) && (localWord.getSuggList().isEmpty()))
      {
        Element localElement2 = request(getUrl(paramString, true));
        if (localElement2 != null)
          fillTranslateSugg(localElement2, localWord);
      }
      localWord.setKey(paramString);
    }
  }

  public Word translateLocal(String paramString, boolean paramBoolean)
  {
    Word localWord = new Word();
    if (StringUtil.isEmpty(paramString));
    while (true)
    {
      return localWord;
      Element localElement1 = requestLocal(getUrl(paramString, false));
      if (localElement1 != null)
        fillTranslate(localElement1, localWord);
      if ((paramBoolean) && (localWord.getSuggList().isEmpty()))
      {
        Element localElement2 = requestLocal(getUrl(paramString, true));
        if (localElement2 != null)
          fillTranslateSugg(localElement2, localWord);
      }
      localWord.setKey(paramString);
    }
  }
}